package com.huike.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 80137
 * @version 1.0
 * @description TODO
 * @date 2023/04/30 11:32
 */
@Component
public class RedisCache {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加Redis缓存
     * @param key 键值
     * @param value 属性
     */
    public void saveCache(String key,String value){
        //删除对应的key
        redisTemplate.delete(key);
        //添加缓存并设置30分钟有效
        redisTemplate.opsForValue().set(key,value,10 , TimeUnit.MINUTES);
    }

    /**
     * 获取缓存值
     * @param key
     * @return
     */
    public String getCache(String key){
        return (String) redisTemplate.opsForValue().get(key);
    }


    /**
     * 删除缓存
     * @param key
     * @return
     */
    public boolean deleteCache(String key){
        return redisTemplate.delete(key);
    }

}
